local meta = FindMetaTable("Player")
local emeta = FindMetaTable("Entity")


function meta:NSteamID()
	local out = self:SteamID()
	out = string.gsub(out, "STEAM", "")
	out = string.gsub(out, "_", "")
	out = string.gsub(out, ":", "")
	return out
end

function meta:GetTeamPaycheck()
	return (Lusion_Teams[self:Team() or 1].salary)
end

-- Below functions is just so I, or others, don't break something with one capital
function meta:GetTeamPayCheck()
	return self:GetTeamPaycheck()
end
function meta:GetPaycheck()
	return self:GetTeamPaycheck()
end
function meta:GetPayCheck()
	return self:GetTeamPaycheck()
end
--[[
	Permissions
		Are you allowed to do things requiring high access, like managing players?
]]--

function meta:IsPermitted()
	return self:IsOwner();
end

--[[
	LusionID
		Player Unique ID based on a system of: first player ever gets id 1, second is 2, etc
]]--
local Lusion_ID = 99999999
if file.Exists("lusion/lusionids/latestid.txt") then
	Lusion_ID = file.Read("lusion/lusionids/latestid.txt")
else	
	Lusion_ID = 0
end
function meta:GiveLusionID()
	Lusion_ID = Lusion_ID + 1
	self:SetNWInt("lusion_id", Lusion_ID)
	tmysql.query("UPDATE roleplaydata SET lusionid='" .. Lusion_ID .. "' WHERE uniqueid='" .. self:NSteamID() .. "'")
	file.Write("lusion/lusionids/latestid.txt", Lusion_ID)	-- Pretty much if I dont do this people will get same ids
end
file.Write("lusion/lusionids/warning.txt", [[Do not modify or delete latestid.txt unless you want to screw up shit]])
function meta:GetLusionID()
	return self:GetNWInt("lusion_id")
end
function meta:NewPlayerID()
	local query1 = tmysql.query("SELECT lusionid FROM roleplaydata WHERE uniqueid='" .. self:NSteamID() .. "'")
	if (query1 == nil or not query1) then
		self:GiveLusionID()
	else
		self:SetNWInt("lusion_id", query1)	-- cuz NW's don't save when they leave :P
	end
end

--[[
	Parsing strings. Yessir do not be mad sir.
		Returns whether something should be a or an.
]]--
-- Job paychecks use this system.
-- Isn't really meta but oh well.
function ParseVowels( str )	-- Too tired right now but later on: True for start of sentence, false or undefined for not start.
	local vowels = {"a","e","i","o","u"}
	local str = string.lower(str)

	if table.HasValue(vowels, string.Left(str, 1)) then
		return "an"
	else
		return "a"
	end
end


--[[
	Simple Job function(s)
		Yeah
]]--
function meta:IsJob(job)
	if ( self:Team() == job ) then
		return true
	end
	
	return false
end
function meta:GetJob()
	return self:Team()
end
function meta:SetJob(job)
	self:SetTeam(job)
end

--[[
	Doors
		Buy doors, sell doors, own doors etc
]]--

function emeta:BuyDoor( pl )
	self.owner = pl
	self:SetDTEntity(1, pl)
end
function emeta:SellDoor( pl )
	self.owner = nil
	self:SetDTEntity(1, nil)
end
function emeta:DoorOwner()
	return self.owner
end
function emeta:DoorHasOwner()
	return (self.owner != nil)
end
--[[
	Some sql
		yeah..
]]--

--[[
		Money System
			Whats the point in a rp gamemode without currency?
]]--
function meta:GetMoney()
	if SERVER then
		return LusionRp_GetMoney( self )
	else
		return self:GetNWInt("currency")
	end
end
function meta:HasMoney(amt)
	return tonumber(self:GetNWInt("currency")) >= amt
end

if SERVER then	
	--[[
		Money System
			Whats the point in a rp gamemode without currency?
	]]--
	function meta:AddMoney(amt)
		self:SetNWInt("currency", (self:GetNWInt("currency") or 0) + math.Round(amt))
		tmysql.query("UPDATE roleplaydata SET money='" .. self:GetNWInt("currency") .. "' WHERE uniqueid='" .. pl:NSteamID() .. "'")
	end
	function meta:TakeMoney(amt)
		self:SetNWInt("currency", self:GetNWInt("currency") - amt)
		tmysql.query("UPDATE roleplaydata SET money='" .. self:GetNWInt("currency") .. "' WHERE uniqueid='" .. pl:NSteamID() .. "'")
	end
	function meta:ResetMoney()
		self:SetNWInt("currency", GAMEMODE.config["Starting Wallet"] or 0)
		tmysql.query("UPDATE roleplaydata SET money='" .. self:GetNWInt("currency") .. "' WHERE uniqueid='" .. pl:NSteamID() .. "'")
	end
	hook.Add("PlayerInitialSpawn", "Paychecking", function(pl)
		timer.Simple(1, function()
			timer.Create( "Give_Paycheck_To_"..pl:GetLusionID(), 45, 0, function()
				hook.Call("OnPlayerPaycheck", GAMEMODE, pl)
				timer.Simple(0.005, function() LusionRp_UpdatePlayer( pl ) end)	-- Hardcoding because you shouldn't need to modify this; timer just in case.
			end)
		end)
	end)
	
	------Index the table
	function emeta:Index()
		for i=1,table.Count(door_table) do
			local explode = string.Explode(",",door_table[i])
			if tostring(self) == explode[1] then
				return i
			else
				return 0
			end
		end
	end
 
	------Door Properties
	function emeta:Ownable()
		return (self:GetDTEntity(1) != nil)
	end
	
end 
--[[
	Adding owner and console checks
		Uh...
]]--
function emeta:IsConsole() if not ValidEntity(self) then return true else return false end end
function meta:IsOwner() return self:GetNWBool("lusion_owner") end

--[[
	Random misc crap
		idk
]]--
function meta:GetMaxRunSpeed()
	return 500
end

print("Loading shared meta")